package com.dh.spring.common;

import com.dh.spring.App;
import com.dh.spring.util.LocalTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.Properties;

/**
 * @Auther: YSFY
 * @Date: 2019/12/1 21:47
 * @Pacage_name: com.dh.spring.common
 * @Project_Name: SERVER
 * @Description: 配置文件读取类
 */
public class PropertiesUtil {
    private static final Logger logger = LoggerFactory.getLogger(PropertiesUtil.class);
    private static Properties pro = null;

    public PropertiesUtil() {
        System.out.println();
        logger.info("[" + LocalTime.formatDate(LocalDateTime.now()) + "] ----------- 开始读取配置文件: server.properties -----------");
        InputStream inputStream =  App.class.getClassLoader().getResourceAsStream("server.properties");
        BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
        try {
            pro = new Properties();
            pro.load(bf);
        } catch (IOException e) {
            logger.error("读取配置文件出现 [IOException] 异常，堆栈信息：{} ", e);
        }
    }

    public String getValue(String key) {
        String value = null;
        try {
            if(null != pro && !pro.isEmpty()) {
                if(pro.containsKey(key)) {
                    return pro.getProperty(key);
                }
            }
        } catch (NumberFormatException e) {
            logger.error("服务端Socket监听端口转换出现 [IOException] 异常，堆栈信息：{} ", e);
        }

        return value;
    }
}
